package com.yy.mall.cart.controller.v1;

import com.yy.mall.cart.service.CartService;
import com.lzy.mall.common.vo.AddToCartVo;
import com.yy.mall.cart.vo.Cart;
import com.yy.mall.cart.vo.CartItem;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * @author lzy
 * @date 2021/9/28 16:08
 */
@RestController
@RequestMapping("v1")
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
@Api(tags = "购物车", value = "购物车")
public class MallCartController {


    private final CartService cartService;

    @GetMapping("/currentUserCartItem")
    @ApiOperation(value = "获取用户购物车", notes = "获取用户购物车")
    public List<CartItem> getCurrentUserCartItem() {
        return cartService.getUserCartItems();
    }

    @ApiOperation(value = "删除购物车商品", notes = "删除购物车商品")
    @GetMapping("/deleteItem")
    public void countItem(@RequestParam("skuId") Long skuId) {
        cartService.deleteItem(skuId);
    }

    @ApiOperation(value = "修改购物车商品数量", notes = "修改购物车商品数量")
    @GetMapping("/countItem")
    public void countItem(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num) {
        cartService.changeItemCount(skuId, num);
    }

    @ApiOperation(value = "选择按钮状态改变", notes = "选择按钮状态改变")
    @GetMapping("/checkItem")
    public void checkItem(@RequestParam("skuId") Long skuId, @RequestParam("check") Integer check) {
        cartService.checkItem(skuId, check);
    }

    /**
     * 浏览器有一个cookie: user-key: 标识用户身份，一个月后过期
     * 如果第一次使用jd的购物车功能，都会给一个临时的用户身份：
     * 浏览器以后保存，每次访问都会带上这个cookie
     * <p>
     * 登录session中有
     * 没登录，按照cookie里面带来的user-key来做
     * 第一次：如果没有临时用户，创建一个临时用户
     *
     * @return
     */
    @ApiOperation(value = "查看购物车状态", notes = "查看购物车状态")
    @GetMapping("/cart")
    public Cart cartListPage() throws ExecutionException, InterruptedException {
        //1、快速得到用户信息，id、user-key
        return cartService.getCart();
    }

    /**
     * 立即加入购物车
     */
    @ApiOperation(value = "立即加入购物车", notes = "立即加入购物车")
    @GetMapping("addToCart")
    public void addToCart(@RequestParam("skuId") Long skuId,
                            @RequestParam("num") Integer num) throws ExecutionException, InterruptedException {
        cartService.addToCart(skuId,num);
    }

    /**
     * 添加商品到购物车
     */
    @ApiOperation(value = "添加商品到购物车", notes = "添加商品到购物车")
    @PostMapping("addToCartAttr")
    public void addToCartAttr(@RequestBody AddToCartVo vo) throws ExecutionException, InterruptedException {
        cartService.addToCartAttr(vo);
    }


    /**
     * 跳到添加购物车成功页
     */
    @ApiOperation(value = "添加购物车成功查询购物车数据", notes = "添加购物车成功查询购物车数据")
    @GetMapping("/addToCartSuccess")
    public CartItem addToCartSuccessPage(@RequestParam("skuId") Long skuId) {
        //查询购物车数据
        return cartService.getCartItem(skuId);
    }
}
